/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Menu.java
 *
 * Created on 21/03/2011, 12:17:54
 */
package vistas;

import comu.Configuracio;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import vistas.textImg.Textos;
import comu.Utilitats;
import extraccionsDades.Extraccions;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author David
 */
public class Menu extends javax.swing.JFrame {

    Administrar administrar;
    General general;
    Trazar trazar;
    Codis codis;

    /** Creates new form Menu */
    public Menu() {
        initComponents();
        Utilitats.centrarVentana(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btSortir = new javax.swing.JButton();
        btConfigurar = new javax.swing.JButton();
        btAdministrar = new javax.swing.JButton();
        btResums = new javax.swing.JButton();
        btGeneral = new javax.swing.JButton();
        btLocalitzar = new javax.swing.JButton();
        btEntrada = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(Textos.MENU_TITOL);

        btSortir.setText(Textos.MENU_BOTO_SORTIR);
        btSortir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSortirActionPerformed(evt);
            }
        });

        btConfigurar.setText(Textos.MENU_BOTO_CONFIGURAR);
        btConfigurar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfigurarActionPerformed(evt);
            }
        });

        btAdministrar.setText(Textos.MENU_BOTO_ADMINISTRAR);
        btAdministrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdministrarActionPerformed(evt);
            }
        });

        btResums.setText(Textos.MENU_BOTO_RESUMS);
        btResums.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btResumsActionPerformed(evt);
            }
        });

        btGeneral.setText(Textos.MENU_BOTO_GENERAL);
        btGeneral.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btGeneralActionPerformed(evt);
            }
        });

        btLocalitzar.setText(Textos.MENU_BOTO_LOCALITZAR);
        btLocalitzar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btLocalitzarActionPerformed(evt);
            }
        });

        btEntrada.setText(Textos.MENU_BOTO_ENTRADA);
        btEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEntradaActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(btConfigurar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(btSortir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(btAdministrar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btLocalitzar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(btEntrada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(btResums, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(btGeneral, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(btGeneral, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btResums, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btEntrada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 55, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btAdministrar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btLocalitzar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btSortir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 41, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btConfigurar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btConfigurarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfigurarActionPerformed
    }//GEN-LAST:event_btConfigurarActionPerformed

    private void btAdministrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdministrarActionPerformed
        try{
            if (administrar == null) {
                administrar = new Administrar(this);
            }
            administrar.update(null, null);
            administrar.setVisible(true);
            this.setVisible(false);
        }catch (Exception e){
            JOptionPane.showMessageDialog(this, Textos.COMU_ERROR_BASE_DADES, "", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }//GEN-LAST:event_btAdministrarActionPerformed

    private void btSortirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSortirActionPerformed
        this.setVisible(false);
        System.exit(0);
    }//GEN-LAST:event_btSortirActionPerformed

    private void btResumsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btResumsActionPerformed
            //Provem lo de exportar a excel
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setSelectedFile(new File("Extraccio" + Configuracio.EXTENSIOEXCEL));
            int selection = fileChooser.showSaveDialog(this);
            if(selection == JFileChooser.APPROVE_OPTION){
                Extraccions.Extraccio(fileChooser.getSelectedFile());
            }
    }//GEN-LAST:event_btResumsActionPerformed

    private void btGeneralActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btGeneralActionPerformed
        try {
            if ( general == null) {
                general = new General(this);
            }
            general.update(null,null);
            general.setVisible(true);
            this.setVisible(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,Textos.COMU_ERROR_BASE_DADES,"",JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }//GEN-LAST:event_btGeneralActionPerformed

    private void btLocalitzarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLocalitzarActionPerformed
        try {
            trazar = new Trazar(this);
            trazar.setVisible(true);
            this.setVisible(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,Textos.COMU_ERROR_BASE_DADES,"",JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }//GEN-LAST:event_btLocalitzarActionPerformed

    private void btEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEntradaActionPerformed
        try {
            if ( codis == null) {
                codis = new Codis(this);
            }else{
                codis.update(null,null);
            }
            codis.setVisible(true);
            this.setVisible(false);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,Textos.COMU_ERROR_BASE_DADES,"",JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }//GEN-LAST:event_btEntradaActionPerformed

    public Image getIconImage() {
        Image retValue = Toolkit.getDefaultToolkit().
        getImage(ClassLoader.getSystemResource("vistas/text_img/icono.png"));


return retValue;
}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Menu().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdministrar;
    private javax.swing.JButton btConfigurar;
    private javax.swing.JButton btEntrada;
    private javax.swing.JButton btGeneral;
    private javax.swing.JButton btLocalitzar;
    private javax.swing.JButton btResums;
    private javax.swing.JButton btSortir;
    // End of variables declaration//GEN-END:variables
}
